package com.eternal.users.service.impl;

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import com.baomidou.mybatisplus.plugins.pagination.PageHelper;
import com.baomidou.mybatisplus.plugins.pagination.Pagination;
import com.eternal.Exception.ResultEnum;
import com.eternal.Exception.ResultException;
import com.eternal.org.entity.OrganizeInfo;
import com.eternal.users.dao.UsersMapper;
import com.eternal.users.service.UsersService;
import com.eternal.users.vo.UsersVo;
import com.eternal.util.page.PageVo;
import com.eternal.util.user.entity.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.NoSuchElementException;

/**
 * <p>
 * 基础组织机构表 服务实现类
 * </p>
 *
 * @author Eternal
 * @since 2018-10-28
 */
@Service
@Primary
@Transactional
public class UsersServiceImpl implements UsersService {

    @Autowired
    private UsersMapper usersMapper;

    @Override
    public List<UsersVo> queryUsersAll() {
        List<UsersVo> usersVos = usersMapper.selectUsersAll();
        return usersVos;
    }

    @Override
    public PageVo<UsersVo> queryUsersAllPage(int current, int size) {
        PageHelper.startPage(current, size);
        List<UsersVo> usersVos = usersMapper.selectUsersAll();
        Pagination pagination = PageHelper.getPagination();
        PageVo<UsersVo> pageVo = new PageVo<>(usersVos, pagination);
        return pageVo;
    }

    @Override
    public PageVo<UsersVo> queryUsersAllPage(int current, int size, String orgId) {
        PageHelper.startPage(current, size);
        List<UsersVo> usersVos = usersMapper.selectUsersAllByOrgId(orgId);
        Pagination pagination = PageHelper.getPagination();
        PageVo<UsersVo> pageVo = new PageVo<>(usersVos, pagination);
        return pageVo;
    }

    @Override
    public PageVo<UsersVo> queryUsersAllPage(int current, int size, String orgId, String parameter) {
        PageHelper.startPage(current, size);
        List<UsersVo> usersVos = usersMapper.selectUsersAllByOrgIdLikeParameter(orgId,parameter);
        Pagination pagination = PageHelper.getPagination();
        PageVo<UsersVo> pageVo = new PageVo<>(usersVos, pagination);
        return pageVo;

    }

    @Override
    public boolean importExcelUsers(MultipartFile file) {

        if (file == null) {
            return false;
        }
        ImportParams params = new ImportParams();
        try {
            List<UserInfo> list = ExcelImportUtil.importExcel(file.getInputStream(), UserInfo.class, params);

            for (UserInfo userInfo : list) {
                boolean insert = userInfo.insertOrUpdate();
            }
        } catch (NoSuchElementException e) {
            throw new ResultException(ResultEnum.IMPORT_EXCEL_NULL_ERROR);
        } catch (IOException e) {
            throw new ResultException(ResultEnum.READ_EXCEL_ERROR);
        } catch (Exception e) {
            throw new ResultException(ResultEnum.IMPORT_ORGANIZE_ERROR);
        }


        return false;
    }


}
